package com.whfc.ms.api.sys.controller;

import com.whfc.common.result.ListData;
import com.whfc.common.result.PageData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.fuum.dto.SysRuleDTO;
import com.whfc.fuum.dto.open.OpenApiLicenseDTO;
import com.whfc.fuum.entity.SysUser;
import com.whfc.fuum.param.open.OpenApiRuleEditParam;
import com.whfc.fuum.service.OpenApiService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 开放平台授权信息
 *
 * @author xugcheng
 * @version 1.0
 * @date 2021-01-04 11:16
 */
@RequiresPermissions("inner/cfg")
@RestController
@RequestMapping("ms/api/cfg/openapi")
public class OpenApiController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @DubboReference(interfaceClass = OpenApiService.class, version = "1.0.0")
    private OpenApiService openApiService;

    @GetMapping(value = "license/list", params = {"pageNum", "pageSize"})
    public Result licenseList(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                              @RequestParam("pageNum") Integer pageNum,
                              @RequestParam("pageSize") Integer pageSize,
                              @RequestParam(value = "keyword", required = false) String keyword) {
        logger.info("查询开放接口授权列表,loginUser:{},pageNum:{},pageSize:{},keyword:{}", user.getUsername(), pageNum, pageSize, keyword);
        PageData<OpenApiLicenseDTO> pageData = openApiService.getOpenApiLicenseList(pageNum, pageSize, keyword);
        return ResultUtil.success(pageData);
    }

    @PostMapping("license/enable/{appKey}")
    public Result licenseEnable(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @PathVariable("appKey") String appKey) {
        logger.info("启用开放接口授权,loginUser:{},appKey:{}", user.getUsername(), appKey);
        openApiService.enableOpenApiLicense(appKey);
        return ResultUtil.success();
    }

    @PostMapping("license/disable/{appKey}")
    public Result licenseDisable(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @PathVariable("appKey") String appKey) {
        logger.info("禁用开放接口授权,loginUser:{},appKey:{}", user.getUsername(), appKey);
        openApiService.disableOpenApiLicense(appKey);
        return ResultUtil.success();
    }

    @GetMapping(value = "license/rule/list", params = "appKey")
    public Result licenseRule(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @RequestParam("appKey") String appKey) {
        logger.info("开放接口应用授权权限,loginUser:{},appKey:{}", user.getUsername(), appKey);
        List<SysRuleDTO> list = openApiService.getOpenApiRules(appKey);
        return ResultUtil.success(new ListData<>(list));
    }

    @GetMapping(value = "license/rule/list", params = "deptId")
    public Result licenseRule(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @RequestParam("deptId") Integer deptId) {
        logger.info("开放接口应用授权权限,loginUser:{},deptId:{}", user.getUsername(), deptId);
        List<SysRuleDTO> list = openApiService.getOpenApiRulesByDeptId(deptId);
        return ResultUtil.success(new ListData<>(list));
    }

    @GetMapping("license/rule/list")
    public Result licenseRuleList(@SessionAttribute(SessionAttr.MS_USER) SysUser user) {
        logger.info("开放接口全部授权权限,loginUser:{}", user.getUsername());
        List<SysRuleDTO> list = openApiService.getOpenApiRules();
        return ResultUtil.success(new ListData<>(list));
    }

    @PostMapping("license/rule/edit")
    public Result licenseRuleEdit(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @RequestBody OpenApiRuleEditParam param) {
        logger.info("编辑开放接口授权权限,loginUser:{},param:{}", user.getUsername(), param);
        openApiService.editOpenApiRules(param);
        return ResultUtil.success();
    }

}
